package MonteCarloSequential;
import GUI.*;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Random;

import javax.swing.*;


public class MonteCarloSimulation {
	public static int progress;
	public static void main(final String[] args) throws NumberFormatException, IOException 
	{
		GUI gui = null;
		GUI.createAndShowGUI();
		
	}
	
	public static double MonteCarloStandardOption(String CallPutFlag, double S, double X, double T, double r, double b, double v, int nSteps)
	{
		double dt, St, Sum = 0, Drift, vSqrdt;
		int i,j,z = 0;
		
		dt = T/nSteps;
		Drift = (b - (Math.pow(v, 2))/2)*dt;
		vSqrdt = v * (Math.sqrt(dt));
		Random rand = new Random();
		
		if(CallPutFlag.equals("c"))
		{
			z = 1;
		}
		else if(CallPutFlag.equals("p"))
		{
			z = -1;
		}
		St = S;
		
		for(j=0;j<nSteps;j++)
		{
			St = St * Math.exp(Drift + (vSqrdt * rand.nextGaussian()));
		}
		Sum = Sum + Math.max(z*(St-X), 0);		
		return ((Math.exp(-r*T))*(Sum));
	}
	
	public static double[] MonteCarloStandardOptionAVG(String CallPutFlag, double S, double X, double T, double r, double b, double v, int nSteps, int nSimulations)
	{
		double tab[]=new double[2];
		double sum = 0;
		final long start = System.nanoTime();
		//System.out.println(S+" "+X+" "+T+" "+r+" "+b+" "+v+" "+nSteps+" "+nSimulations);
		for(int i=0 ; i<nSimulations;i++)
		{
			sum = MonteCarloStandardOption(CallPutFlag, S, X, T, r, b, v, nSteps) + sum;
			if(i==nSimulations/2)
				progress=50;
			
		}
		progress=100;
		final long end = System.nanoTime();
	//	System.out.println("Time (seconds) taken " + (end - start)/1.0e9);
		tab[0]=sum/nSimulations;
		tab[1]=(end - start)/1.0e9;
		return tab;


}
	
}


